<template>
	<view>
		<!-- <text>登录页面</text> -->
		<view class="login-container">
			<uni-icons type="contact-filled" size='100'></uni-icons>
			<button type="primary" class="btn-login" @click="getUserInfo">一键登录</button>
			<view class="tips-text">
				登录后享受更多权益
			</view>
		</view>
	</view>
</template>

<script>
	import {mapMutations,mapState} from 'vuex';
	import {token_data} from '@/api/user-request.js';
	export default {
		name:"my-login",
		data() {
			return {
				
			};
		},
		computed:{
			// ...mapState('user_m',['navigateInfo'])
			...mapState('user_m',['navigateInfo']),
		},
		methods:{
			...mapMutations('user_m',['updateUserInfo','updateToken','updateNavigateInfo']),
			getUserInfo(){
				console.log(this);
				const that = this;
				uni.getUserProfile({
					desc:"获取你的昵称、头像、地区及性别",
					// success(info){
					// 	console.log(info);
					// 	console.log(111);
					// },
					success(info) {
						console.log(info);
						console.log(111);
						that.updateUserInfo(info);
						that.getToken(info);
					},
					fail(err) {
						console.log(err);
						console.log(222);  
					}
				})
			},
			getToken(info){
				let that = this;
				const result = uni.login({
					provider: 'weixin', //使用微信登录
					success: async (loginRes)=> {
					    console.log(loginRes);
						
						const query = {
							encryptedData:info.encryptedData,
							rawData:info.rawData,
							iv:info.iv,
							signature:info.signature,
							code:loginRes.code,
						}
						console.log(query);
						const result = await token_data(query);
						console.log(result);
						if(result.errMsg == 'request:ok'){
							let token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo';
							that.updateToken(token);
							
							this.navigateBack();
						}
					},
					fail(err) {
						console.log(err); 
						return uni.$showMsg('登录失败');
					}
				})
			},
			navigateBack(){
				console.log(this.navigateInfo);
				if(this.navigateInfo && this.navigateInfo.navigateType == 'switchTab'){
					uni.switchTab({
						url:this.navigateInfo.from,
						complete:()=>{
							console.log('从登录页面跳转回购物车');
							this.updateNavigateInfo(null);
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
.login-container{
	height: 750rpx;
	// width: 20px;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	padding: 0 20px;
	.btn-login{
		width: 100%;
		border-radius: 45px;
	}
}
</style>
